package com.iscas.green.extralindextest.config;

import com.iscas.green.extralindextest.servlet.CustomServlet;
import com.iscas.green.extralindextest.servlet.SecurityFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Servlet配置类
 * ServletComponentScan注解启用对WebServlet注解的扫描
 */
@Configuration
@ServletComponentScan(basePackages = "com.iscas.green.extralindextest.servlet")
public class ServletConfig {

    /**
     * 手动注册一个自定义Servlet
     */
    @Bean
    public ServletRegistrationBean<CustomServlet> customServletRegistrationBean() {
        ServletRegistrationBean<CustomServlet> registrationBean = 
                new ServletRegistrationBean<>(new CustomServlet(), "/servlet/custom/*");
        registrationBean.setName("customServlet");
        // 设置Servlet初始化参数
        registrationBean.addInitParameter("paramName", "paramValue");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }
    
    /**
     * 手动注册一个安全过滤器
     */
    @Bean
    public FilterRegistrationBean<SecurityFilter> securityFilterRegistrationBean() {
        FilterRegistrationBean<SecurityFilter> registrationBean = 
                new FilterRegistrationBean<>(new SecurityFilter());
        
        // 设置过滤器优先级，数字越小优先级越高
        registrationBean.setOrder(100);
        
        // 设置过滤器名称
        registrationBean.setName("securityFilter");
        
        // 设置过滤器参数
        registrationBean.addInitParameter("secretHeaderName", "X-API-Secret");
        
        // 设置过滤的URL模式
        registrationBean.addUrlPatterns("/servlet/secure/*");
        
        return registrationBean;
    }
} 